package com.suxin.algorithm.queue;

/**
 * @author Tang
 * @classname ArrayQueue
 * @description [ 队列-数组 ]
 * @date 2022/4/13 23:04
 */
public class ArrayQueue {

    /**
     * 数组最大存储的个数
     */
    private int maxSize;

    /**
     * 队头
     */
    private int front;

    /**
     * 队尾
     */
    private int rear;

    /**
     * 数据
     */
    private int[] array;

    public ArrayQueue(int arrMaxSize) {
        this.maxSize = arrMaxSize;
        array = new int[arrMaxSize];
        front = -1;
        rear = -1;
    }

    public void addQueue(int i) {
        if (full()) {
            System.out.println("queue full !");
            return;
        }
        rear++;
        array[rear] = i;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("empty queue!");
        }
        front++;
        return array[front];
    }

    private boolean isEmpty() {
        /**
         * 队头 == 队尾
         */
        return front == rear;
    }

    private boolean full() {
        // 队尾 + 1 = 最大容量
        return rear == maxSize - 1;
    }

}